package com.tuzhi.test;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DataSourceConfig {
    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource")
    public DruidDataSource defaultDataSource() {
        return (DruidDataSource) DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
    @Bean
    @ConfigurationProperties(prefix = "my.datasource")
    public DruidDataSource myDataSource() {
        return (DruidDataSource) DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Bean("myJdbcTemplate")
    public JdbcTemplate getJdbcTemplate(@Qualifier("myDataSource") DruidDataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
